<template>
  <div class="register_container">
    <div class="register_box">
      <!-- 注册表单标题区域 -->
      <div class="register_title">
        <div class="title_tips">欢迎注册</div>
      </div>
      <!--注册表单区域 -->
      <el-form
        label-width="0px"
        class="register_form"
        ref="registerFormRef"
        status-icon
        :model="registerForm"
        :rules="registerFormRules"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-yonghu"
            v-model="registerForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-mima"
            v-model="registerForm.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      registerForm: {
        username: "",
        password: "",
      },
      registerFormRules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: ["blur", "change"],
          },
          {
            min: 4,
            max: 18,
            message: "长度在 4 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    resetRegisterForm() {
      this.$refs.registerFormRef.resetFields();
    },
    register() {
      this.$refs.registerFormRef.validate(async (valid) => {
        if (!valid) return;

        const { data: check } = await this.$http.post("login/deterUser", this.registerForm);
        if (check.code == 200) {
          return this.$message.error(check.data.tips)
        }
        const { data: res } = await this.$http.post("login/registerUsername", this.registerForm);
        if (res.code != 100) return this.$message.error("注册失败！");
        else this.$message.success("注册成功");

        // 将权限和用户id放入session域中
        this.$store.commit("edit", this.res.data.user);
        this.$router.push("/login");
      });
    },
  },
};
</script>

<style lang="scss">
.register_container {
  background: url("../assets/login_images/background.jpg") center center fixed no-repeat;
  background-size: cover;
  height: 100%;
  text-align: left;
}

.register_box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 80%;
  top: 45%;
  transform: translate(-50%, -50%);

  .title {
    font-size: 30px;
    font-weight: 500;
    color: rgba(14, 18, 26, 1);
    padding-left: 20px;
  }

  .title_tips {
    margin-top: 29px;
    font-size: 25px;
    font-weight: 400;
    color: #5ccfc8;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-left: 20px;
  }
}

.register_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  .el-input {
    border-radius: none;
  }
  input {
    background-color: #f5f3fb;
    height: 50px;
  }
}

.btns {
  .el-button {
    width: 50%;
    height: 50px;
  }
}
</style>
